<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\captcha\Captcha;


class Register extends Controller
{
    public function register()
    {

        return view();
    }

    public function doRegister()
    {
        $param = input('post.');   //使用input助手
        if (empty($param['email'])) {
            $this->error('email不能为空');
        }
        //邮箱格式验证
        if (!filter_var(trim($param['email']), FILTER_VALIDATE_EMAIL)) {
            $this->error('邮箱格式错误');
        }
        if (empty($param['passw1'])) {
            $this->error('passw1不能为空');
        }
        if (strlen($param['passw1']) < 3) {
            $this->error('密码长度不能小于3');
        }
        if (empty($param['passw2'])) {
            $this->error('passw2不能为空');
        }
        if ($param['passw1'] != $param['passw2']) {
            $this->error('两次输入密码不一致!');
        }
        $captcha = new Captcha();
        if (!$captcha->check($param['code'])) {
            $this->error('验证码错误');
        }

        $data = db('tb_member')->where('email', $param['email'])->find();  //使用了db助手
        //$data = Db::table('tb_member')->where('email', $param['email'])->find();
        if (!empty($data)) {
            $this->error('该用户已被注册！');
        } else {
            $result = Db::execute("insert into tb_member(email,password,jifen,ye) values('" . $param['email'] . "','" . md5($param['passw1']) . "',0,0)");
            //             dump($result);
            $this->success('注册成功！');
            $this->redirect(url('login/login'));
        }
    }

    public function checkEmail()
    {
        $sql = "select * from tb_member where email = '" . input('post.email') . "'";
        $cnt = count(Db::query($sql));
        if ($cnt) { //重复
            return "您输入的Email已经存在！";
        } else {
            return "恭喜你，邮箱还未注册";
        }
    }
}
